
import PointRoom from "../../Entities/PointRoom";
import PoolRoom from "../../Entities/PoolRoom";
import DealRoom from "../../Entities/DealRoom";
import UserSeat from "./UserSeat"

cc.Class({
    extends: UserSeat,

    properties: {
        faceSprite:{
            default:null,
            type:cc.Sprite
        },

        labelName:{
            default:null,
            type:cc.Label
        },

        ptsNode:{
            default:null,
            type:cc.Node
        },

        labelPTSScore:{
            default:null,
            type:cc.Label
        },

        handScoreNode:{
            default:null,
            type:cc.Node
        },

        labelHandScore:{
            default:null,
            type:cc.Label
        },

        moneyNode:{
            default:null,
            type:cc.Node
        },

        labelMoney:{
            default:null,
            type:cc.Label
        },

        progressBar:{
            default:null,
            type:cc.ProgressBar
        },

        labelTotalTime:{
            default:null,
            type:cc.Label
        },

        labelNowTime:{
            default:null,
            type:cc.Label
        },

        iconReady:{
            default:null,
            type:cc.Node
        },

        netNode:{
            default:null,
            type:cc.Node
        },

        countDownNode:{
            default:null,
            type:cc.Node
        },

        headBg:{
            default:null,
            type:cc.Node
        },

        faceMask:{
            default:null,
            type:cc.Node
        },

        coinIcon:{
            default:null,
            type:cc.Node
        },

        dropMask:{
            default:null,
            type:cc.Node
        }
    },

    changeFaceBg:function () {
        let colorString = cc.gg.dataMgr.getPlayerInfo().face_bg
        cc.log("我方座位头像背景颜色变为：",colorString)
        this.headBg.color = (new cc.color()).fromHEX("#"+colorString)
    },

    changeNet:function (isGood) {
        if(isGood){
            cc.find("Good",this.netNode).active = true
            cc.find("Off",this.netNode).active = false
        }else {
            cc.find("Good",this.netNode).active = false
            cc.find("Off",this.netNode).active = true
        }
    },

    onLoad () {
        this._super()
        this._setFaceWhite()
        this.countDownNode.active = false
        this.iconReady.active = false
        this.coinIcon.active = !cc.gg.gameDataUtil.showRupee
        cc.find("Good",this.netNode).active = true
        cc.find("Off",this.netNode).active = false

        cc.gg.registeListener("update_bottoms_scores",this._onUpdateBottomScore.bind(this),this)
        // this.updateGameScore(0)
    },

    // initSeat:function (userObj) {
    //     if(!userObj){
    //
    //     }
    // },

    _onUpdateBottomScore:function (playerId) {
        cc.log("===============更新底部手牌分数")

        if(!this.playerInfo){
            cc.warn("TPPHead is not been initialized!")
            return
        }
        if(!this.labelHandScore)
            return

        if(this.playerInfo.id !== playerId) {
            cc.log("隐藏了一个labelHandScore")
            this.labelHandScore.node.active = false
            return
        }

        let seqCount = 0
        let hasPure = false
        for(let i=0;i<cc.gg.tpgMgr.pokerBottoms.length;i++){
            let bot = cc.gg.tpgMgr.pokerBottoms[i]
            if(!bot.type){
                continue
            }

            if(bot.type.type === cc.gg.enumConfig.SeperateType.SEQUENCE){
                if(bot.type.pure) {
                    // cc.log("===有pure")
                    hasPure = true
                }
                seqCount++
            }
        }
        // cc.log("===共有顺子",seqCount)

        let scoreTotal = 0
        for(let i=0;i<cc.gg.tpgMgr.pokerBottoms.length;i++){
            let bot = cc.gg.tpgMgr.pokerBottoms[i]
            if(!bot.type){
                let s = bot.node.getComponent("SeperateBottom").bottomSum || 0
                scoreTotal += s
                // cc.log("组分数：",s)
                continue
            }

            if(bot.type.type === cc.gg.enumConfig.SeperateType.SEQUENCE && bot.type.pure){
                continue
            }

            if(seqCount > 1 && hasPure){
                if(bot.type.type === cc.gg.enumConfig.SeperateType.SEQUENCE) continue
                if(bot.type.type === cc.gg.enumConfig.SeperateType.SET) continue
            }

            let s = cc.gg.sortUtil.GET_CARDS_SUM(bot.pokers) || 0
            scoreTotal += s
            // cc.log("组分数：",s)
        }

        // let scoreTotal = 0
        // for(let i=0;i<cc.gg.tpgMgr.pokerBottoms.length;i++){
        //     let s = cc.gg.tpgMgr.pokerBottoms[i].node.getComponent("SeperateBottom").bottomSum || 0
        //     cc.log("组分数：",s)
        //     scoreTotal += s
        // }
        scoreTotal = Math.min(scoreTotal,80)
        scoreTotal = scoreTotal > 80 ? 80 : scoreTotal
        this.labelHandScore.string = scoreTotal
    },

    start () {

    },

    update (dt) {
        // if(this._startCountting){
        //     this.changeTime(dt)
        // }
    },

    initTPPHead:function (userInfo,roomType) {
        this.playerInfo = userInfo
        this.node.active = true

        this.labelName.string = userInfo.name
        if(cc.gg.dataMgr.room instanceof PointRoom || roomType === cc.gg.enumConfig.RoomType.POINTS){
            if(this.ptsNode) this.ptsNode.active = false
            if(this.moneyNode){
                this.moneyNode.active = true
                if(cc.gg.gameDataUtil.showRupee){
                    let isGuild = cc.gg.dataMgr.playGuild
                    if(isGuild){
                        let str = userInfo.coin ? cc.gg.gameDataUtil.getStringOfRupee(userInfo.coin) : cc.gg.gameDataUtil.getStringOfRupee(userInfo.deposit + userInfo.winnings)
                        this.labelMoney.string = str
                    }else {
                        let room = cc.gg.dataMgr.room
                        let isPractice = (room.mode & 0xf) === 0
                        if(room && isPractice){
                            this.labelMoney.string = "₹ 0"
                        }else {
                            let str = userInfo.coin ? cc.gg.gameDataUtil.getStringOfRupee(userInfo.coin) : cc.gg.gameDataUtil.getStringOfRupee(userInfo.deposit + userInfo.winnings)
                            this.labelMoney.string = str
                        }
                    }
                }else {
                    this.labelMoney.string = userInfo.coin ? userInfo.coin : userInfo.deposit + userInfo.winnings
                }
            }
        }else if(cc.gg.dataMgr.room instanceof PoolRoom || roomType === cc.gg.enumConfig.RoomType.POOL){
            if(this.ptsNode){
                this.ptsNode.active = true
                this.labelPTSScore.string = "PTS:0"
            }
            this.moneyNode.active = false
            this.updateGameScore(userInfo.score || 0)
        }else if(cc.gg.dataMgr.room instanceof DealRoom || roomType === cc.gg.enumConfig.RoomType.DEAL){
            if(this.ptsNode){
                this.ptsNode.active = true
            }
            this.moneyNode.active = false
            cc.log("TPPHead的score是：",userInfo.score)
            this.updateGameScore(userInfo.score || 0)
        }

        if(this.handScoreNode) this.labelHandScore.string = 0

        this.netNode.active = true

        cc.gg.logicUtil.initHead(userInfo.img,this.faceSprite)
        this.changeFaceBg()
        this.initSeatState(userInfo.b_state)
    },

    initSeatState:function (bstate) {
        cc.log("update mine seat's b_state:",bstate)
        if(bstate === cc.gg.enumConfig.ReadyState.ELIMINATED){
            this.changeToEliminated()
        }
        // if(bstate === cc.gg.enumConfig.ReadyState.DROPED){
        //     this.changeToEliminated()
        // }
        else {
            this._setFaceWhite()
        }
    },

    changeToEliminated:function () {
        this.statusLabel.string = "Eliminated"
        this._setFaceGray()
    },

    changeToDrop:function () {
        this.statusLabel.string = ""
        this._setFaceDrop()
    },

    startCountting:function (countSeconds) {
        this.countDownNode.active = true
        this._noticePlayed = false
        this._startCountting = true
        this.progressBar.node.active = true
        this.progressBar.progress = 1

        this.totalSecond = countSeconds
        this.nowSecond = countSeconds

        this.labelTotalTime.string = this.nowSecond
        this.labelNowTime.string = this.nowSecond
    },

    stopCountting:function () {
        this.countDownNode.active = false
        this._noticePlayed = false
        this._startCountting = false
        this.progressBar.node.active = false
    },

    changeToEmpty:function () {
        this.playerInfo = null
        this.node.active = false

        this.labelName.string = ""
        this.labelPTSScore.string = "PTS:0"
        // this.updateGameScore(0)
        if(this.labelMoney){
            this.labelMoney.string = ""
            cc.log("清理后的",this.labelMoney.string)
        }
        this.faceSprite.spriteFrame = null

        this.netNode.active = false
    },

    changeCoin:function (newCoin) {
        cc.log(">>>>>>>>>>>>>设置coin")
        let room = cc.gg.dataMgr.room
        let isPractice = (room.mode & 0xf) === 0
        if(room && isPractice && cc.gg.gameDataUtil.showRupee){
            if(this.labelMoney) this.labelMoney.string = "₹ 0"
        }else {
            if(this.labelMoney) this.labelMoney.string = cc.gg.gameDataUtil.getStringOfRupee(newCoin)
        }
    },

    changeTime : function (dt) {
        this.nowSecond = dt
        if(this.nowSecond <= 0){
            this.nowSecond = 0
            return
        }

        if(this.nowSecond < 5 && !this._noticePlayed){
            this._noticePlayed = true
            cc.find("Canvas/SoundManager").getComponent("SoundManager").playTimeNoticeSound()
        }

        this.progressBar.progress = this.nowSecond/this.totalSecond
        this.labelNowTime.string = parseInt(this.nowSecond)
    },
});
